package com.sixdog.regulate.cmdService;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Date;

public class HttpUtils {

    public static final String TAO_BAO = "taobao";
    public static final String JING_DONG = "jingdong";


    //参考taobao时间进行矫正
    public static void correctTimeWithServer(Date date,String dateServerName) {
        String url;
        switch (dateServerName){
            case TAO_BAO : {
                url = "http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp";
                break;
            }
            case JING_DONG :{
                url = "https://api.m.jd.com/client.action?functionId=queryMaterialProducts&client=wh5";
                break;
            }
            default:{
                url = "";
            }
        }



        long time = date.getTime();
        long lastTime = time;

        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        // 创建Get请求
        HttpGet httpGet = new HttpGet(url);
        // 响应模型
        CloseableHttpResponse response = null;
        //服务器时间
        long t=0;
        try {
            //打上开始时间标记
            long startMil = System.currentTimeMillis();
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null && response.getStatusLine().getStatusCode()==200) {
                JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(responseEntity));
                //打上结束时间标记
                long endMil = System.currentTimeMillis();
                //通过 从服务器得到的时间 加上延迟，算出此时此刻的服务器时间
                switch (dateServerName){
                    case TAO_BAO : {
                        t = Long.valueOf(jsonObject.getJSONObject("data").get("t").toString())+(endMil-startMil);
                        break;
                    }
                    case JING_DONG :{
                        t = Long.valueOf(jsonObject.getJSONObject("currentTime2").toString())+(endMil-startMil);
                        break;
                    }
                }

//                System.out.println("从发起请求到得到时间数据:"+(endMil-startMil));
                //调整后的时间=调整前的时间 +（系统时间-服务器时间）
                lastTime = time + (endMil - t);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        date.setTime(lastTime);
        System.out.println("时间调整变化值："+(lastTime-time));
    }






}
